<?php

namespace app\company\controller;

use think\Exception;

class Project extends Common
{
    // 检车项目管理
    public function index()
    {
        $data = [];
        $model = new \app\company\model\Project();
        //列表数据
        $list = $model->getList();
        $data['list'] = $list;
        $data['vehicle'] = db('vehicle')->select();
        return view('', $data);
    }
    // 添加
     public function add()
    {
        $data = [];
        $data['vehicle'] = db('vehicle')->select();
        $inspection_id = db('vehicle_inspection')->distinct(true)->column('inspection_id');
        $data['project'] = db('inspection')->whereIn('id', $inspection_id)->select();
        return view('', $data);
    }
    //执行添加
    public function addproject()
    {
        $data = input('post.');
        $model = new \app\company\model\Project();
        try {
            $model->doAdd_project($data);
        } catch (Exception $e) {
            return json([
                'statusCode' => 300,
                'message' => $e->getMessage(),
            ]);
        }
        return json([
            'statusCode' => 200,
            'message' => '添加成功',
            'navTabId' => 'project_manage',
            'callbackType' => 'closeCurrent',
        ]);
    }
    // 执行删除
    public function doDelete()
    {
        $userId = input('get.id');
        $Project = new \app\company\model\Project();
        try {
            $Project->doDelete($userId);
        } catch (Exception $e) {
            return json([
                'statusCode' => 300,
                'message' => $e->getMessage(),
            ]);
        }
        return json([
            'statusCode' => 200,
            'message' => '删除成功',
            'navTabId' => 'project_manage',
        ]);
    }

    // 修改
    public function modify()
    {
        $id = input('get.id');
        $info = db('vehicle_inspection_company')
            ->where('id',$id)
            ->where('company_id', session('company.company_id'))
            ->find();
        // $info = Db::view('cd_vehicle_inspection', 'goods_name,license_type')
        //     ->view('cd_vehicle_inspection_company', 'id, shop_price, sort', 'cd_vehicle_inspection.goods_id=cd_vehicle_inspection_company.inspection_id')
        //     ->where('goods_id',$id)
        //     ->where('company_id', session('company.company_id'))
        //     ->find();
        $data['info'] = $info;
        $data['vehicle'] = db('vehicle')->where('id', '=', $info['license_type'])->value('title');
        $this->assign('id',$info['id']);
        return view('', $data);
    }

    // 执行修改
    public function domodify()
    {
        $data = input('post.');
        try {
            $model = new \app\company\model\Project();
            $model->doModify_project($data);
        } catch (Exception $e) {
            return json([
                'statusCode' => 300,
                'message' => $e->getMessage(),
            ]);
        }
        return json([
            'statusCode' => 200,
            'message' => '修改成功',
            'navTabId' => 'project_manage',
            'callbackType' => 'closeCurrent',
        ]);
    }
}